#region License
// ShammatehCrudViewModel.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Shammateh.Win.UI.ViewModels
{
    using System.Collections.Generic;
    using System.Linq;
    using Xoqal.Data;
    using Xoqal.Data.Linq;
    using Xoqal.Presentation.ViewModels;

    /// <summary>
    /// Represents the base functionality for a maize simple view-model CRUD operation.
    /// </summary>
    public abstract class ShammatehCrudViewModel<T> : CrudViewModel<T>, IMasterDetailContent
        where T : class, new()
    {
        #region Fields

        private bool isOperationPanelExpanded;
        private IShammatehMasterDetailCrudViewModel masterViewModel;

        #endregion

        #region Contructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CrudViewModel&lt;T&gt;"/> class.
        /// </summary>
        public ShammatehCrudViewModel(IUnitOfWork unitOfWork, IRepository<T> repository)
            : base(unitOfWork, repository)
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether this instance is operation panel expanded.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is operation panel expanded; otherwise, <c>false</c>.
        /// </value>
        public bool IsOperationPanelExpanded
        {
            get 
            {
                return this.isOperationPanelExpanded; 
            }

            set
            {
                if (this.isOperationPanelExpanded == value)
                {
                    return;
                }

                this.isOperationPanelExpanded = value;
                this.RaisePropertyChanged(() => this.IsOperationPanelExpanded);
            }
        }

        /// <summary>
        /// Gets or sets the master view model.
        /// </summary>
        /// <value>The master view model.</value>
        public IShammatehMasterDetailCrudViewModel MasterViewModel
        {
            get 
            {
                return this.masterViewModel; 
            }

            set
            {
                if (this.masterViewModel == value)
                {
                    return;
                }

                this.masterViewModel = value;
                this.RaisePropertyChanged(() => this.MasterViewModel);
            }
        }

        #endregion

        #region Protected Methods

        protected override void OnCurrentItemChanged()
        {
            base.OnCurrentItemChanged();
            if (!this.HasCurrentItem)
            {
                this.IsOperationPanelExpanded = false;
            }
        }

        /// <summary>
        /// Called when add command execute.
        /// </summary>
        protected override void OnAddCommandExecute()
        {
            base.OnAddCommandExecute();
            this.IsOperationPanelExpanded = true;
            if (this.MasterViewModel != null)
            {
                this.MasterViewModel.OperationTabControlSelectedIndex = 0;
                this.MasterViewModel.IsOperationPanelExpanded = true;
            }
        }

        /// <summary>
        /// Called when edit command execute.
        /// </summary>
        protected override void OnEditCommandExecute()
        {
            base.OnEditCommandExecute();
            this.IsOperationPanelExpanded = true;
            if (this.MasterViewModel != null)
            {
                this.MasterViewModel.OperationTabControlSelectedIndex = 0;
                this.MasterViewModel.IsOperationPanelExpanded = true;
            }
        }

        /// <summary>
        /// Gets the data items.
        /// </summary>
        /// <param name="startIndex">The start index.</param>
        /// <param name="itemCount">The item count.</param>
        /// <param name="sortDescriptions">The sort descriptions.</param>
        /// <returns></returns>
        protected override IEnumerable<T> GetDataItems(int startIndex, int itemCount, SortDescription[] sortDescriptions)
        {
            return this.GetQuery().ToPage(startIndex, itemCount, sortDescriptions);
        }

        /// <summary>
        /// Gets the data item count.
        /// </summary>
        /// <returns></returns>
        protected override int GetDataItemCount()
        {
            return this.GetQuery().Count();
        }

        protected abstract IQueryable<T> GetQuery();

        #endregion
    }
}
